class DataController < ApplicationController
  require 'fastercsv/lib/faster_csv'

  def get_csv(flickrs,slideshares,tweets,youtubes)
    csv_string = FasterCSV.generate do |csv|
      csv << ["Site","Title","Description","User","URL","Date"]

      if flickrs != nil
        @n = 1
        flickrs.each do |flickr|
          csv << ["Flickr","#{flickr.title}","#{flickr.tags}","#{flickr.owner}","http://www.flickr.com/photos/#{flickr.owner}#{flickr.photoid}","#{flickr.upload_day.to_s}/#{flickr.upload_month}/#{flickr.upload_year}"]
          @n = @n + 1
        end
      end

      if slideshares != nil
        @n = 1
        slideshares.each do |slideshare|
          csv << ["Slideshare","#{slideshare.title}","#{slideshare.description}","#{slideshare.username}","#{slideshare.url}","#{slideshare.upload_day}/#{slideshare.upload_month}/#{slideshare.upload_year}"]
         @n = @n + 1
        end
      end

      if tweets != nil
        @n = 1
        tweets.each do |tweet|
          csv << ["Twitter","#{tweet.twitter_status_id}","#{tweet.text}","#{tweet.in_reply_to_screen_name}","http://twitter.com/#{tweet.in_reply_to_screen_name}/statuses/#{tweet.twitter_status_id}","#{tweet.upload_day}/#{tweet.upload_month}/#{tweet.upload_year}"]
          @n = @n + 1
        end
      end

      if youtubes != nil
        @n = 1
        youtubes.each do |youtube|
          csv << ["Youtube","#{youtube.title}","#{youtube.description}","#{youtube.author}","#{youtube.player_url}","#{youtube.upload_day}/#{youtube.upload_month}/#{youtube.upload_year}"]
          @n = @n + 1
        end
      end
    end
    return csv_string
  end

  def all_csv
    @sql = "WHERE tag_id=#{params[:id]}"
  
    if params[:site] == "flickr" || params[:site] == "all"
      @flickrs = Flickr.find_by_sql("SELECT * FROM flickrs #{@sql}")
    end
    if params[:site] == "slideshare" || params[:site] == "all"
      @slideshares = Slideshare.find_by_sql("SELECT * FROM slideshares #{@sql}")
    end
    if params[:site] == "twitter" || params[:site] == "all"
      @tweets = Tweet.find_by_sql("SELECT * FROM tweets #{@sql}")
    end
    if params[:site] == "youtube" || params[:site] == "all"
      @youtubes = Youtube.find_by_sql("SELECT * FROM youtubes #{@sql}")
    end
    csv_string = get_csv(@flickrs,@slideshares,@tweets,@youtubes)
    send_data(csv_string,:type => 'text/csv; charset=utf-8; header=present',:filename => "Year_Data.csv")
  end

  def year_csv
    @year = params[:y].to_i
    @month = params[:m].to_i
    @counter = 0
    if @month == 1
      @sql = "WHERE tag_id=#{params[:id]} AND upload_year=#{@year}"
    else
      @sql = "WHERE tag_id=#{params[:id]} AND (upload_year=#{@year} AND upload_month>=#{@month}) OR (upload_year=#{(@year+1)} AND upload_month<#{@month})"
    end
  
    if params[:site] == "flickr" || params[:site] == "all"
      @flickrs = Flickr.find_by_sql("SELECT * FROM flickrs #{@sql}")
    end
    if params[:site] == "slideshare" || params[:site] == "all"
      @slideshares = Slideshare.find_by_sql("SELECT * FROM slideshares #{@sql}")
    end
    if params[:site] == "twitter" || params[:site] == "all"
      @tweets = Tweet.find_by_sql("SELECT * FROM tweets #{@sql}")
    end
    if params[:site] == "youtube" || params[:site] == "all"
      @youtubes = Youtube.find_by_sql("SELECT * FROM youtubes #{@sql}")
    end
    csv_string = get_csv(@flickrs,@slideshares,@tweets,@youtubes)
    send_data(csv_string,:type => 'text/csv; charset=utf-8; header=present',:filename => "Year_Data.csv")
  end

  def month_csv
    @year = params[:y].to_i
    @month = params[:m].to_i
    @counter = 0
    @sql = "WHERE tag_id=#{params[:id]} AND upload_year=#{@year} AND upload_month=#{@month}"
    if params[:site] == "flickr" || params[:site] == "all"
      @flickrs = Flickr.find_by_sql("SELECT * FROM flickrs #{@sql}")
    end
    if params[:site] == "slideshare" || params[:site] == "all"
      @slideshares = Slideshare.find_by_sql("SELECT * FROM slideshares #{@sql}")
    end
    if params[:site] == "twitter" || params[:site] == "all"
      @tweets = Tweet.find_by_sql("SELECT * FROM tweets #{@sql}")
    end
    if params[:site] == "youtube" || params[:site] == "all"
      @youtubes = Youtube.find_by_sql("SELECT * FROM youtubes #{@sql}")
    end  

    csv_string = get_csv(@flickrs,@slideshares,@tweets,@youtubes)
    send_data(csv_string,:type => 'text/csv; charset=utf-8; header=present',:filename => "Month_Data.csv")
  end

  def day_csv
    @year = params[:y].to_i
    @month = params[:m].to_i
    @day = params[:d].to_i
    @sql = "WHERE tag_id=#{params[:id]} AND upload_year=#{@year} AND upload_month=#{@month} AND upload_day=#{@day}"
    puts "@sql = "+@sql
    if params[:site] == "flickr" || params[:site] == "all"
      @flickrs = Flickr.find_by_sql("SELECT * FROM flickrs #{@sql}")
    end
    if params[:site] == "slideshare" || params[:site] == "all"
      @slideshares = Slideshare.find_by_sql("SELECT * FROM slideshares #{@sql}")
    end
    if params[:site] == "twitter" || params[:site] == "all"
      @tweets = Tweet.find_by_sql("SELECT * FROM tweets #{@sql}")
    end
    if params[:site] == "youtube" || params[:site] == "all"
      @youtubes = Youtube.find_by_sql("SELECT * FROM youtubes #{@sql}")
    end

    csv_string = get_csv(@flickrs,@slideshares,@tweets,@youtubes)
    send_data(csv_string,:type => 'text/csv; charset=utf-8; header=present',:filename => "Day_Data.csv")
  end
end
